﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;

namespace tc58News
{
	public class Producer
	{
		private Channel shared;

		public Producer(Channel channel)
		{
			this.shared = channel;
		}

		public void Produce(object obj)
		{
			HtmlSource sourct = (HtmlSource)obj;
			WebRequest req = WebRequest.Create(sourct.SourceUrl);
			req.Method = "get";
			WebResponse result = req.GetResponse();
			Stream sm = result.GetResponseStream();
			StreamReader sr = new StreamReader(sm, Encoding.UTF8);
			string html = sr.ReadToEnd();

			Regex reg = new Regex("href=\"newslist.aspx\\?.*?page=([0-9]{1,})\">末页</a>");
			Regex regpage = new Regex("<div class=\"abq\">\\s+<a href=\"(.*?)\">");
			Match mc = reg.Match(html);
			int page = int.Parse(mc.Groups[1].Value);

			string urls = sourct.SourceUrl + "&page={0}";


			for (int i = 1; i < page; i++)
			{
				Console.WriteLine(i.ToString()+"/"+page.ToString());
				req = WebRequest.Create(string.Format(urls, i));
				try
				{
					result = req.GetResponse();

					sm = result.GetResponseStream();

					sr = new StreamReader(sm, Encoding.GetEncoding("gb2312"));

					string htmls = sr.ReadToEnd();

					MatchCollection mcs = regpage.Matches(htmls);

					for (int j = 0; j < mcs.Count; j++)
					{
						string url = "http://www.58tc.cn/" + mcs[j].Groups[1].Value;
						HtmlSource entity = new HtmlSource()
						{
							SourceUrl = url,
							CateType = sourct.CateType,
							Content = ""
						};
						shared.SetBuffer(entity);

						//Console.WriteLine(i.ToString() + "/" + page.ToString()+"/"+j.ToString());
					}
				}
				catch
				{
				}
				finally
				{
				}

			}




			
		}
	}
}
